package repositories;

import java.util.Collection;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.Painting;

@Repository
public interface PaintingRepository extends JpaRepository<Painting,Integer> {
	
	// List the paintings owned by a customer
	@Query("select o.ownedPainting from Ownership o where o.legalOwner.id = ?1 and o.finishDate = null")
	Collection<Painting> findPaintingsByOwnerId(int ownerId);
	
	// List the paintings in a gallery
	@Query("select p from Painting p where p.gallery.id = ?1")
	Collection<Painting> findPaintingsByGalleryId(int galleryId);
	
	// List the paintings that doesn't belong to any gallery
	@Query("select o.ownedPainting from Ownership o where o.legalOwner.id = ?1 " +
			"and o.ownedPainting.gallery = null and o.finishDate = null")
	Collection<Painting> findPaintingsWithoutGalleryAndByPrincipal(int ownerId);
	
	//List the paintings with more comments
	@Query("select p from Painting p where p.comments.size = (select max(pAux.comments.size) from Painting pAux)")
	Collection<Painting> findPaintingWithMoreComments();

}
